<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-present http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\pay\controller;

use cmf\controller\AdminBaseController;
use think\db\Query;
use think\facade\Db;

class LotteryController extends AdminBaseController
{

    /**
     * Notes:转盘商品列表
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function index()
    {
        $param = $this->request->param();
        $list = Db::name("lottery_config")
            ->where(function (Query $query) use ($param) {
                if (isset($param['title']) && !empty($param['title'])) {
                    $query->where('name', 'like', "%" . $param['title'] . "%");
                }
            })
            ->order("id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('title', isset($param['title']) ? $param['title'] : '');
        return $this->fetch();
    }

    /**
     * Notes:添加转盘商品页面
     * User:
     * DateTime: 2022-01-17 16:53
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * Notes:执行添加转盘商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function addPost()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['moneys'] = $param['moneys'];
        $data['prize'] = $param['prize'];
        $data['winningrate'] = $param['winningrate'];
        $data['img'] = $param['img'];
        $data['create_time'] = date("Y-m-d H:i:s");
        $res = Db::name("lottery_config")->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:编辑转盘商品页面
     * User:
     * DateTime: 2022-01-17 17:11
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $param = $this->request->param();
        $info = Db::name("lottery_config")->where(["id" => $param['id']])->find();
        $this->assign("info", $info);
        return $this->fetch();
    }

    /**
     * Notes:执行编辑转盘商品
     * User:
     * DateTime: 2022-01-17 16:58
     */
    public function editPost()
    {
        $param = $this->request->param();
        $data['name'] = $param['name'];
        $data['moneys'] = $param['moneys'];
        $data['prize'] = $param['prize'];
        $data['winningrate'] = $param['winningrate'];
        $data['img'] = $param['img'];
        $data['update_time'] = time();
        $res = Db::name("lottery_config")->where(["id" => $param['id']])->save($data);
        if (!$res) $this->error("失败！");
        $this->success("成功！");
    }

    /**
     * Notes:删除
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function delete()
    {
        $id = $this->request->param('id', 0, 'intval');
        Db::name("lottery_config")->where('id', $id)->delete();
        $this->success("删除成功");
    }

    /**
     * Notes:转盘订单
     * User:
     * DateTime: 2022-01-17 14:57
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public function order()
    {
        $param = $this->request->param();
        $list = Db::name("lottery_prize")->alias("o")
            ->field("o.*,p.name,p.img,u.user_login")
            ->join("lottery_config p", "o.lottery_id = p.id", "left")
            ->join("user u", "o.user_id = u.id", "left")
            ->where(function (Query $query) use ($param) {
                if (isset($param['title']) && !empty($param['title'])) {
                    $query->where('p.name', 'like', "%" . $param['title'] . "%");
                }
                if (isset($param['user_id']) && !empty($param['user_id'])) {
                    $query->where(['o.user_id' => $param['user_id']]);
                }
                if (isset($param['start_time']) && !empty($param['start_time'])) {
                    if (!isset($param['end_time']) || empty($param['end_time'])) {
                        $param['end_time'] = time();
                    }
                    $query->whereTime('o.create_time', 'between', [$param['start_time'], $param['end_time']]);
                }
            })
            ->order("o.id desc")
            ->paginate(30);
        $page = $list->render();
        $this->assign("page", $page);
        $this->assign("list", $list);
        $this->assign('title', isset($param['title']) ? $param['title'] : '');
        $this->assign('user_id', isset($param['user_id']) ? $param['user_id'] : '');
        $this->assign('start_time', isset($param['start_time']) ? $param['start_time'] : '');
        $this->assign('end_time', isset($param['end_time']) ? $param['end_time'] : '');
        return $this->fetch();
    }

    /**
     * Notes:删除中奖记录
     * User:
     * DateTime: 2022-01-17 15:10
     * @throws \think\Exception
     */
    public function deleteOrder()
    {
        $id = $this->request->param('id', 0, 'intval');
        Db::name("lottery_prize")->where('id', $id)->delete();
        $this->success("删除成功");
    }
}
